package zym.diandian.module.sys;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ConfigurationInfo;
import android.os.AsyncTask;
import android.os.Build;
import android.os.SystemClock;
import android.telephony.TelephonyManager;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import zym.diandian.tools.CpuHelper;
import zym.diandian.tools.DisplayHelper;
import zym.diandian.tools.HardwareHelper;
import zym.diandian.tools.NetworkHelper;
import zym.diandian.tools.OsHelper;
import zym.diandian.tools.Tools;

public class GetInfoTask extends AsyncTask<Void, Void, List<String>> {

    private Context mAct;
    private ListView mLv;

    private SysinfoAdapter adapter = null;
    private int item_height;

    public GetInfoTask(Context activity, ListView view, int item_height) {
        this.mLv = view;
        this.mAct = activity;
        this.item_height = item_height;
    }

    @SuppressLint("NewApi")
    @Override
    protected List<String> doInBackground(Void... voids) {
        // TODO Auto-generated method stub
        List<String> mDataList = new ArrayList<>();

        ActivityManager am = (ActivityManager) mAct.getSystemService(Context.ACTIVITY_SERVICE);
        ConfigurationInfo deviceConfigurationInfo = am.getDeviceConfigurationInfo();
        mDataList.add("@基本信息");
        mDataList.add("品牌 @  " + android.os.Build.BRAND);
        mDataList.add("型号 @  " + android.os.Build.MODEL);
        mDataList.add("Android@ " + "" + android.os.Build.VERSION.RELEASE + " (" + android.os.Build.VERSION.SDK_INT + ")");
        String device = "" + Build.DEVICE;
        mDataList.add("设备@ " + device);
        mDataList.add("@CPU 信息");
        mDataList.add("CPU型号@ " + CpuHelper.getProcessor());
        mDataList.add("CPU核心@ " + CpuHelper.getNumCores());
        mDataList.add("CPU位数@ " + CpuHelper.getProcessBit());
        mDataList.add("最低频率@ " + CpuHelper.getMinCpuFreq());
        mDataList.add("最高频率@ " + CpuHelper.getMaxCpuFreq());
        mDataList.add("当前频率@ " + CpuHelper.getCurCpuFreq());
        //mDataList.add("屏幕尺寸@  " + DisplayHelper.getScreenSize(mAct));
        int[] resolution = DisplayHelper.getResolution(mAct);
        mDataList.add("@屏幕信息");
        mDataList.add("分辨率@ " + resolution[0] + "x" + resolution[1]);
        //mDataList.add("屏幕密度@ " + DisplayHelper.getDensityDpi(mAct));

        mDataList.add("主摄像头@ " + HardwareHelper.getCameraSize(0));
        mDataList.add("前置摄像头@ " + HardwareHelper.getCameraSize(1));

        mDataList.add("OpenGL ES Version @  " + deviceConfigurationInfo.getGlEsVersion());
        //mDataList.add("GPU渲染器@ " + Static.Global.GL_RENDERER);
        //mDataList.add("GPU供应商@ " + Static.Global.GL_VENDOR);
        //mDataList.add("GPU版本@ " + Static.Global.GL_VERSION);

        mDataList.add("@网络 信息");
        TelephonyManager manager = (TelephonyManager) mAct.getSystemService(Context.TELEPHONY_SERVICE);
        try {
            mDataList.add("运营商@ " + NetworkHelper.getYYS(Integer.parseInt(manager.getSimOperator().trim())));
        } catch (Exception e) {
            mDataList.add("运营商@ UNKNOWN");
        }
        mDataList.add("移动网络类型@ " + NetworkHelper.getNetworkType(mAct, manager.getNetworkType()) + "");
        try {
            mDataList.add("手机类型@ " + NetworkHelper.getPhoneType(manager.getPhoneType()));
        } catch (Exception e) {
            mDataList.add("手机类型@ UNKNOWN");
        }
        mDataList.add("IMEI@ " + manager.getDeviceId());

        //mDataList.add("信号@ " + Static.Global.PHONE_DBM);
        mDataList.add("SIM状态@ " + NetworkHelper.getSimState(manager.getSimState()));
        String imsi = manager.getSubscriberId();
        mDataList.add("IMSI@ " + imsi);
        mDataList.add("漫游@ " + ((manager.isNetworkRoaming()) ? "漫游中" : "非漫游"));
        mDataList.add("主机@ " + DisplayHelper.getProp("net.hostname"));
        mDataList.add("IP@ " + NetworkHelper.getMIP());
        mDataList.add("DNS1@ " + DisplayHelper.getProp("net.dns1"));
        mDataList.add("DNS2@ " + DisplayHelper.getProp("net.dns2"));
        mDataList.add("@电池信息");
        String battery[] = OsHelper.getBatteryInfo();
        try {
            mDataList.add("电池电量@ " + battery[0]);
            mDataList.add("电池电压@ " + battery[1]);
            mDataList.add("电池温度@ " + battery[2]);
            mDataList.add("电池类型@ " + battery[3]);
            mDataList.add("电池健康@ " + battery[4]);
            mDataList.add("电池状态@ " + battery[5]);
            if (battery[5].contains("充电中") || battery[5].contains("满")) {
                mDataList.add("充电类型@ " + battery[6]);
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        mDataList.add("@版本信息");
        if (Build.VERSION.SDK_INT >= 14) {
            mDataList.add("基带版本@ " + android.os.Build.getRadioVersion());
        }
        mDataList.add("Java Vm @  " + System.getProperty("java.vm.name", "Dalvik") + System.getProperty("java.vm.version", "1.6.0"));
        mDataList.add("Linux@ " + System.getProperty("os.version") + " (" + Build.VERSION.INCREMENTAL + ")");
        mDataList.add("硬件@ " + android.os.Build.HARDWARE);
        mDataList.add("厂商@ " + android.os.Build.MANUFACTURER);
        mDataList.add("指纹@ " + android.os.Build.FINGERPRINT);
        mDataList.add("编译时间@ " + Tools.time(android.os.Build.TIME, "yyyy年MM月dd日 a"));
        mDataList.add("版本号@ " + android.os.Build.ID);
        mDataList.add("序列号@ " + android.os.Build.SERIAL);

        StringBuilder builder = new StringBuilder();
        android.support.v4.util.TimeUtils.formatDuration(SystemClock.elapsedRealtime(), builder);
        String startupTime = builder.toString();
        startupTime = startupTime.replace("+", "");
        startupTime = startupTime.replace("d", "天");
        startupTime = startupTime.replace("h", "小时");
        startupTime = startupTime.replace("ms", "毫秒");
        startupTime = startupTime.replace("m", "分");
        startupTime = startupTime.replace("s", "秒");

        mDataList.add("开机时长@ " + startupTime);
        //onProgressUpdate();

        return mDataList;
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        adapter = new SysinfoAdapter(mAct, null, item_height);
        mLv.setAdapter(adapter);
    }

    @Override
    protected void onPostExecute(List<String> mDataList) {
        // TODO Auto-generated method stub
        super.onPostExecute(mDataList);
        adapter = null;
        adapter = new SysinfoAdapter(mAct, mDataList, item_height);
        mLv.setAdapter(adapter);
    }

}
